package com.fs.dua.service.impl;

import com.fs.common.utils.DynamicSqlHelper;
import com.fs.dua.bean.dto.ApiDTO;
import com.fs.dua.bean.query.ApiQuery;
import com.fs.dua.service.ApiService;
import com.fs.dua.service.DuaService;
import jakarta.el.MethodNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class DuaServiceImpl implements DuaService {
    @Autowired
    private ApiService apiService;

    /**
     * 执行对应的接口
     *
     * @param params 请求参数
     */
    @Override
    public Object execute(String key, Map<String, Object> params) {
        ApiQuery q = new ApiQuery();
        q.setKey(key);
        ApiDTO api = apiService.findOne(q).orElseThrow(MethodNotFoundException::new);

        String sql = api.getContent().trim();
        if (log.isDebugEnabled()) {
            log.debug("Prepare to execute sql: {}, params: {}", sql, params);
        }

        return DynamicSqlHelper.executeSql("dua-" + api.getId(), sql, params);
    }

    public static void main(String[] args) {
        String sql = "with as update a set b = 1";
        System.out.println(sql.matches(".*update .* set .*"));
    }
}
